package com.abocode.aichat.app.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by guanxf on 2015/12/16.
 */
public class SHAUtils {
      private static final String algorithm = "SHA-256";

        public static  String toSHA256(String plainText) {

            MessageDigest md = null;
            try {
                md = MessageDigest.getInstance(algorithm);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            assert md != null;
            md.update(plainText.getBytes());
            byte[] b = md.digest();
            StringBuilder output = new StringBuilder(32);
            for (byte aB : b) {
                String temp = Integer.toHexString(aB & 0xff);
                if (temp.length() < 2) {
                    output.append("0");
                }
                output.append(temp);
            }
            return output.toString();
        }
}
